//
//  ViewController.swift
//  YSSpreadSheetView
//
//  Created by 万孟超 on 05/09/2023.
//  Copyright (c) 2023 万孟超. All rights reserved.
//

import UIKit
import YSSpreadSheetView

class ViewController: UIViewController {
    
    lazy var spreadSheetView: YSSpreadSheetView = {
        let config = YSSpreadSheetViewConfig()
        config.isShowSectionTitle = true
        config.isShowItemTitle = true
        config.sectionHeight = YSSpreadSheetViewConfig.automaticDimension
        config.estimatedSectionHeight = 44
        config.isAverageSheet = false
        config.totalHighLightCountInView = 8
        config.totalHighLightCountInSection = 2
        config.boundsToView = true
        let spread = YSSpreadSheetView(config: config, delegate: self, dataSource: self)
        return spread
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        installSubviews()
    }
    
    func installSubviews() {
        view.addSubview(spreadSheetView)
        spreadSheetView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        spreadSheetView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        spreadSheetView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        spreadSheetView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }

}

extension ViewController: YSSpreadSheetViewDelegate, YSSpreadSheetViewDataSource {
    
    func numberOfSections(in spreadSheetView: YSSpreadSheetView) -> Int {
        return 9
    }
    
    func numberOfItems(in spreadSheetView: YSSpreadSheetView) -> Int {
        return 9
    }
    
    func spreadSheetView(_ spreadSheetView: YSSpreadSheetView, cellAt indexPath: IndexPath) -> YSSpreadSheetSubView? {
        let cell = YSSpreadSheetSubView()
        let section = indexPath.section
        let item = indexPath.item
        cell.sheetTitleLabel.text = "(\(section + 1), \(item + 1))"
        cell.sheetTitleLabel.textAlignment = .center
        return cell
    }
    
    func spreadSheetView(_ spreadSheetView: YSSpreadSheetView, titleViewForSectionAt section: Int) -> YSSpreadSheetSubView? {
        let cell = YSSpreadSheetSubView()
        cell.sheetTitleLabel.text = "序号:\(section + 1)"
        cell.sheetTitleLabel.textAlignment = .center
        return cell
    }
    
    func spreadSheetView(_ spreadSheetView: YSSpreadSheetView, titleViewForItemAt item: Int) -> YSSpreadSheetSubView? {
        let cell = YSSpreadSheetSubView()
        cell.sheetTitleLabel.text = "第\(item + 1)列"
        cell.sheetTitleLabel.textAlignment = .center
        return cell
    }
    
    func spreadSheetView(_ spreadSheetView: YSSpreadSheetView, widthForCellAt item: Int) -> CGFloat {
        return 50
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, shouldHighlightAt indexPath: IndexPath) -> Bool {
        true
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, didHighlightAt indexPath: IndexPath) {
        
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, shouldUnHighlightAt indexPath: IndexPath) -> Bool {
        true
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, didUnHighlightAt indexPath: IndexPath){
        
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        true
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, shouldDeselectItemAt indexPath: IndexPath) -> Bool {
        true
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, didSelectItemAt indexPath: IndexPath) {
        
    }
    
    func spreadSheetView(_ spreadSheetViewCell: YSSpreadSheetSubView, deselectItemAt indexPath: IndexPath) {
        
    }
}


